package com.xlongwei.accounts.dao;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
import org.noear.solon.annotation.Inject;
import org.noear.solon.test.SolonTest;

import com.xlongwei.accounts.App;
import com.xlongwei.accounts.model.Record;
import com.xlongwei.accounts.service.RecordService;
import com.xlongwei.accounts.view.Query;
import com.xlongwei.accounts.view.Report;

import features.SqlTest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SolonTest(App.class)
@SuppressWarnings({"rawtypes"})
public class RecordDaoTest {
    @Inject
    RecordDao recordDao;
    @Inject
    ExtraDao extraDao;
    @Inject
    RecordService recordService;

    @Test
    public void list() throws Throwable {
        SqlTest.testAndRollback(() -> {
            recordService.income(1, 1, 200, 1, 0, LocalDate.now(), null);
            recordService.transfer(1, 3, 100, 1, 2, 1, LocalDate.now(), "继续");
            Query query = new Query();
            query.setUserid(1);
            query.setWithRemark(true);
            query.setRemark("继");;
            List<Record> list = recordDao.list(query);
            list.stream().forEach(System.out::println);
            extraDao.list(list.stream().map(Record::getRecordid).collect(Collectors.toList()))
                    .values().stream().forEach(System.out::println);
        });
    }

    @Test
    public void report() throws Throwable {
        SqlTest.testAndRollback(() -> {
            recordService.income(1, 1, 200, 1, 1, LocalDate.now(), null);
            recordService.outcome(1, 2, 100, 1, 1, LocalDate.now(), null);
            recordService.transfer(1, 3, 100, 1, 2, 1, LocalDate.now(), "");
            Report report = new Report();
            report.setUserids(Arrays.asList(1,2));
            report.setYear("2025");
            report.setAccountids(Arrays.asList(1,2));
            report.setMemberids(Arrays.asList(1,2));
            report.setLedgerids(Arrays.asList(1,2));
            List<Map> list = recordDao.report(report);
            list.forEach(System.out::println);

            System.out.println("-----");
            report.setGroupBy("ledger");
            list = recordDao.report(report);
            list.forEach(System.out::println);
            
            System.out.println("-----");
            report.setGroupBy("member");
            list = recordDao.report(report);
            list.forEach(System.out::println);
            
            System.out.println("-----");
            report.setGroupBy("account");
            list = recordDao.report(report);
            list.forEach(System.out::println);
        });
    }
}
